#include"head.h"
int main(){
    ///服务器ip ------配置文件                    TODO
    char  ip[100]={0};
    char port[100]={0} ;
    getparameter("ip",ip);
    getparameter("port",port);

    //建立连接 socket 拿到fd
    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);

    // 建立连接
    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(atoi(port));
    sockaddr.sin_addr.s_addr = inet_addr(ip);
    int ret_conn=connect(socket_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
    ERROR_CHECK(ret_conn,-1,"ret_conn");
    //服务器连接之后 客户端输入 需要监听输入 stdin

    //add_epoll(fd_epoll,socket_fd);
    sendMsg msg;
    client_status user;
    //  init_struct(msg,user);
    int flag=login(socket_fd,&user);
    while(1){
        //解析命令
        char bufs[1024];

        bzero(bufs,sizeof(bufs));
        int read_ret=read(STDIN_FILENO,bufs,sizeof(bufs));
        ERROR_CHECK(read_ret,-1,"read_ret");
        //删除回车
        int len = strlen(bufs);
        len = len-1;
        bufs[len]='\0';
        //解剖 命令 空格切开
        char *savebuf={0};
        char *token=strtok_r(bufs," ",&savebuf);
     //   printf("path_last:%s\n",savebuf);
        //报错当前路径                    
      //  strcpy(user.path,"");
       // printf("path: %s\n",user.path);
        //保存 后续切割路径
        strcpy(user.path_last,savebuf);
      //  printf("name = %s,  path = %s\n", user.user_name, user.path);
        if(strcmp(token,"ls")==0){
            my_ls(msg,user,socket_fd);
        }
        else if(strcmp(token,"cd")==0){

            my_cd(msg,&user,socket_fd);

        }else if(strcmp(token,"rm")==0){

            my_rm(msg,user,socket_fd);

        }else if(strcmp(token,"puts")==0){

            my_puts(msg,user,socket_fd);

        }else if(strcmp(token,"gets")==0){

            my_gets(msg,user,socket_fd);

        }else if(strcmp(token,"pwd")==0){

            my_pwd(msg,user,socket_fd);    
        }else {
            printf("错误命令！！\n");
        }
        printf("path = %s\n", user.path);
    }

}



